<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Chapter1 R语言知识汇总 | 小蓝哥的知识荒原</title>
<meta name="author" content="李详">
<!-- JS --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.2"></script><script src="https://kit.fontawesome.com/6ecbd6c532.js" crossorigin="anonymous"></script><script src="libs/header-attrs-2.9/header-attrs.js"></script><script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="libs/bootstrap-4.6.0/bootstrap.min.css" rel="stylesheet">
<script src="libs/bootstrap-4.6.0/bootstrap.bundle.min.js"></script><script src="libs/bs3compat-0.2.5.1/tabs.js"></script><script src="libs/bs3compat-0.2.5.1/bs3compat.js"></script><link href="libs/bs4_book-1.0.0/bs4_book.css" rel="stylesheet">
<script src="libs/bs4_book-1.0.0/bs4_book.js"></script><script src="https://cdn.jsdelivr.net/autocomplete.js/0/autocomplete.jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/mark.js@8.11.1/dist/mark.min.js"></script><!-- CSS -->
</head>
<body data-spy="scroll" data-target="#toc">

<div class="container-fluid">
<div class="row">
  <header class="col-sm-12 col-lg-3 sidebar sidebar-book"><a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>

    <div class="d-flex align-items-start justify-content-between">
      <h1>
        <a href="index.html" title="">小蓝哥的知识荒原</a>
      </h1>
      <button class="btn btn-outline-primary d-lg-none ml-2 mt-1" type="button" data-toggle="collapse" data-target="#main-nav" aria-expanded="true" aria-controls="main-nav"><i class="fas fa-bars"></i><span class="sr-only">Show table of contents</span></button>
    </div>

    <div id="main-nav" class="collapse-lg">
      <form role="search">
        <input id="search" class="form-control" type="search" placeholder="Search" aria-label="Search">
</form>

      <nav aria-label="Table of contents"><h2>Table of contents</h2>
        <ul class="book-toc list-unstyled">
<li><a class="" href="index.html">简介</a></li>
<li><a class="active" href="r.html"><span class="header-section-number">1</span> R语言知识汇总</a></li>
<li><a class="" href="python.html"><span class="header-section-number">2</span> Python知识汇总</a></li>
<li><a class="" href="bioinf.html"><span class="header-section-number">3</span> 生物信息学</a></li>
<li><a class="" href="literature.html"><span class="header-section-number">4</span> 嗑盐文献</a></li>
<li><a class="" href="article.html"><span class="header-section-number">5</span> 嗑盐文章</a></li>
<li><a class="" href="%E4%B8%8A%E8%AF%BE%E7%AC%94%E8%AE%B0.html"><span class="header-section-number">6</span> 上课笔记</a></li>
<li><a class="" href="other.html"><span class="header-section-number">7</span> Other</a></li>
<li><a class="" href="references.html">References</a></li>
</ul>

        <div class="book-extra">
          <p><a id="book-repo" href="https://github.com/lixiang117423/lixiang117423.github.io">View book source <i class="fab fa-github"></i></a></p>
        </div>
      </nav>
</div>
  </header><main class="col-sm-12 col-md-9 col-lg-7" id="content"><div id="r" class="section level1" number="1">
<h1>
<span class="header-section-number">Chapter1</span> R语言知识汇总<a class="anchor" aria-label="anchor" href="#r"><i class="fas fa-link"></i></a>
</h1>
<div id="本章前言" class="section level2" number="1.1">
<h2>
<span class="header-section-number">1.1</span> 本章前言<a class="anchor" aria-label="anchor" href="#%E6%9C%AC%E7%AB%A0%E5%89%8D%E8%A8%80"><i class="fas fa-link"></i></a>
</h2>
<p>本章主要是关于R语言的相关知识，包括R语言基础知识、数据统计分析、数据可视化及机器学习等内容。</p>
</div>
<div id="pac4xiang" class="section level2" number="1.2">
<h2>
<span class="header-section-number">1.2</span> R包pac4xiang的使用<a class="anchor" aria-label="anchor" href="#pac4xiang"><i class="fas fa-link"></i></a>
</h2>
<p><code>pac4xiang</code>这个R包是我自己开发的，包含了一些我自己常用的函数。包目前在<a href="https://github.com/lixiang117423/pac4xiang">GitHub</a>上，更多功能还在完善中，待进一步开发。</p>
<div id="安装" class="section level3" number="1.2.1">
<h3>
<span class="header-section-number">1.2.1</span> 安装<a class="anchor" aria-label="anchor" href="#%E5%AE%89%E8%A3%85"><i class="fas fa-link"></i></a>
</h3>
<p>在R中运行下面的代码安装<code>pac4xiang</code>。</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">devtools</span><span class="fu">::</span><span class="fu"><a href="https://devtools.r-lib.org//reference/remote-reexports.html">install_github</a></span><span class="op">(</span><span class="st">'https://github.com/lixiang117423/pac4xiang'</span><span class="op">)</span></code></pre></div>
</div>
<div id="使用方法" class="section level3" number="1.2.2">
<h3>
<span class="header-section-number">1.2.2</span> 使用方法<a class="anchor" aria-label="anchor" href="#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95"><i class="fas fa-link"></i></a>
</h3>
<div id="calstandcurve" class="section level4" number="1.2.2.1">
<h4>
<span class="header-section-number">1.2.2.1</span> <code>calStandCurve</code><a class="anchor" aria-label="anchor" href="#calstandcurve"><i class="fas fa-link"></i></a>
</h4>
<p>该函数用与计算相对定量qPCR的标曲。qPCR标曲点样参照图<a href="r.html#fig:qPCR1">1.1</a>。按照我的思路和习惯，我会准备8个浓度梯度的cDNA,从高到低分别是C0到C8。C0是对应批次下多有样品cDNA的组合，然后再以一定的稀释倍数稀释得到C1，C1再以同样的倍数稀释得到C2，依此类推，直到得到C8。</p>
<div class="figure" style="text-align: center">
<span id="fig:qPCR1"></span>
<img src="figures/pac4xiang%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E/1.png" alt="qPCR标曲点样示意图" width="585"><p class="caption">
图1.1: qPCR标曲点样示意图
</p>
</div>
<p>函数具体怎么用呢？参考下方的代码：</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">df.1</span> <span class="op">&lt;-</span> <span class="fu">calStandCurve</span><span class="op">(</span>
  data <span class="op">=</span> <span class="st">"20210927下机数据/20210928lx_1.txt"</span>下机<span class="co"># 下机数据位置</span>NA<span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"OsPOX223"</span>, <span class="st">"OsAPX1"</span>, <span class="st">"OsAPX2"</span>, <span class="st">"OsPAL"</span><span class="op">)</span>,<span class="co"># 基因名称,与点样表顺序对应</span>NA<span class="op">=</span> <span class="fl">3</span>, <span class="co"># 技术重复的数量</span>NA<span class="op">=</span> <span class="fl">4</span>, <span class="co"># 稀释倍数</span>NA<span class="op">=</span> <span class="fl">2</span>, <span class="co"># 从第几行开始计算</span>NA<span class="op">=</span> <span class="fl">6</span>, <span class="co"># 到第几行终止计算</span>NA<span class="op">=</span> <span class="cn">TRUE</span>, <span class="co"># 是否剔除空值</span>NA<span class="op">=</span> <span class="st">"pdf"</span> <span class="co"># 保存的图片的文件格式</span>NA<span class="op">)</span></code></pre></div>
<p>其中基因的数量和技术重复的数量乘起来必须是12；基因的顺序必须是从左到右的；输入稀释倍数后告诉程序后续的标曲建立如何取相应的对数；<code>start</code>表示从第几行开始计算，如<code>start = 2</code>表示的是从B行开始计算标曲；对应的<code>end = 6</code>表示的是到第6行，也就是F行结束，也就意味着第一行和最后两行其实是不用的。默认是保存标曲的图片的，如果不需要保存图片加上<code>save.fig = FALSE</code>即可，默认的图片格式是<code>pdf</code>格式，可以自定义。</p>
<div class="figure" style="text-align: center">
<span id="fig:qPCR2"></span>
<img src="figures/pac4xiang%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E/3.png" alt="函数calStandCurve返回的数据框" width="293"><p class="caption">
图1.2: 函数calStandCurve返回的数据框
</p>
</div>
<p>返回的结果是个数据框（参考图<a href="r.html#fig:qPCR2">1.2</a>），包含了基因名称、标曲公式、斜率、截距、R<span class="math inline">\(^2\)</span>、P值、该标曲适用的自变量范围（最大值和最小值）及计算标曲的日期等。保存的图片默认存在当前工作目录下（运行<code>getcwd()</code>查看当前R语言工作目录）。</p>
</div>
<div id="calrtqpcr" class="section level4" number="1.2.2.2">
<h4>
<span class="header-section-number">1.2.2.2</span> <code>calRTqPCR</code><a class="anchor" aria-label="anchor" href="#calrtqpcr"><i class="fas fa-link"></i></a>
</h4>
<p>该函数在<code>calStandCurve</code>函数返回的标曲结果的基础上可以计算不同处理下各基因的表达情况。样品的点样方法参考图<a href="r.html#fig:qPCR3">1.3</a>。</p>
<div class="figure" style="text-align: center">
<span id="fig:qPCR3"></span>
<img src="figures/pac4xiang%E4%BD%BF%E7%94%A8%E8%AF%B4%E6%98%8E/2.png" alt="qPCR标曲点样示意图" width="544"><p class="caption">
图1.3: qPCR标曲点样示意图
</p>
</div>
<p>计算表达量的代码如下：</p>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">exp.1</span> <span class="op">&lt;-</span> <span class="fu">calRTqPCR</span><span class="op">(</span>
  data <span class="op">=</span> <span class="st">"20210927下机数据/20210929lx_1.txt"</span>机<span class="co"># 下机数据位置</span>NA<span class="op">=</span> <span class="va">df.1</span>,<span class="co">#标曲对应的数据框，上一步的输出结果</span>NA<span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"OsPOX223"</span>, <span class="st">"OsAPX1"</span>, <span class="st">"OsAPX2"</span>, <span class="st">"OsPAL"</span><span class="op">)</span>,<span class="co"># 基因名称,与点样表顺序对应</span>NA<span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"CK"</span>, <span class="st">"Inter"</span>, <span class="st">"Infect"</span><span class="op">)</span> <span class="co"># 三个不同的处理分别是什么</span>NA<span class="op">)</span></code></pre></div>
<p>其中的<code>data</code>就的样品的下机导出数据，<code>StandCurve</code>是函数<code>calStandCurve</code>返回的数据框，<code>gene</code>需要严格控制大小写，必须和输入函数<code>calStandCurve</code>的基因名称一样，<code>treatment</code>是指定从左到右的处理分别是什么，这个是为了后续方便进行<em><code>t</code></em>检验。</p>
</div>
<div id="getalignresults" class="section level4" number="1.2.2.3">
<h4>
<span class="header-section-number">1.2.2.3</span> <code>getAlignResults</code><a class="anchor" aria-label="anchor" href="#getalignresults"><i class="fas fa-link"></i></a>
</h4>
<p>该函数可以将软件<code>CLUSTALW</code>的输出文件变成<code>.fasta</code>格式的比对结果。
写这个函数的原因在于每次我用MEGA进行序列比对构建进化树的时候都报错，不是字符串有问题就是长度不对，索性直接用Linux系统下的<code>CLUSTALW</code>进行序列比对，然后用MEGA构建进化树，百试不爽！</p>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">my.aln</span> <span class="op">=</span> <span class="fu">getAlignResults</span><span class="op">(</span>aln <span class="op">=</span> <span class="st">'test.aln'</span><span class="op">)</span></code></pre></div>
<p>运行完成后会在当前目录下生成一个<code>YourAlignResults.fasta</code>文件，就是<code>fasta</code>格式的比对文件，然后就可以用来构建进化树了。
- <code>mult.aov</code>
该函数用于多分组数据进行方差分析。</p>
<ul>
<li>
<code>multGroupTtest</code>
该函数用于进行多分组的<code>t</code>检验。
这个函数是用于进行多组<code>t</code>检验的。比如每次跑qPCR，会有多个处理多个目的基因，这个时候就需要关注每个目的基因在不同的处理中的表达量。用法参考下方代码：</li>
</ul>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">t.test</span> <span class="op">&lt;-</span> <span class="fu">multGroupTtest</span><span class="op">(</span>
  data <span class="op">=</span> <span class="va">exp.all.final</span>, <span class="co"># 输入矩阵</span>NA<span class="op">=</span> <span class="st">"Gene"</span>, <span class="co"># 第一个分组名称</span>NA<span class="op">=</span> <span class="st">"Treatment"</span>, <span class="co"># 第二个分组名称</span>NA<span class="op">=</span> <span class="st">"CK"</span>, <span class="co"># 指定CK是叫什么</span>NA<span class="op">=</span> <span class="st">"Expression"</span>, <span class="co"># 指定用于比较的数值在哪一列</span>NA<span class="op">=</span> <span class="fl">0.95</span> <span class="co"># 指定检验水平</span>NA<span class="op">)</span></code></pre></div>
</div>
<div id="plot96well" class="section level4" number="1.2.2.4">
<h4>
<span class="header-section-number">1.2.2.4</span> <code>plot96well</code><a class="anchor" aria-label="anchor" href="#plot96well"><i class="fas fa-link"></i></a>
</h4>
<p>该函数用于可视化<code>Roche</code>96孔板的<code>Cq</code>值。</p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu">plot96well</span><span class="op">(</span>data <span class="op">=</span> <span class="st">"20210927下机数据/20210928lx_1.txt"</span>NA<span class="op">)</span>NA</code></pre></div>
</div>
<div id="plotciselement" class="section level4" number="1.2.2.5">
<h4>
<span class="header-section-number">1.2.2.5</span> <code>plotCisElement</code><a class="anchor" aria-label="anchor" href="#plotciselement"><i class="fas fa-link"></i></a>
</h4>
<p>该函数用与可视化基因启动子上的顺式作用元件。需要输入的参数有三个：</p>
</div>
<div id="dataplantcare返回的文件" class="section level4" number="1.2.2.6">
<h4>
<span class="header-section-number">1.2.2.6</span> <code>data</code>：<code>plantCARE</code>返回的文件；<a class="anchor" aria-label="anchor" href="#dataplantcare%E8%BF%94%E5%9B%9E%E7%9A%84%E6%96%87%E4%BB%B6"><i class="fas fa-link"></i></a>
</h4>
</div>
<div id="length启动子长度" class="section level4" number="1.2.2.7">
<h4>
<span class="header-section-number">1.2.2.7</span> <code>length</code>：启动子长度；<a class="anchor" aria-label="anchor" href="#length%E5%90%AF%E5%8A%A8%E5%AD%90%E9%95%BF%E5%BA%A6"><i class="fas fa-link"></i></a>
</h4>
</div>
<div id="cis需要展示哪些顺式作用元件" class="section level4" number="1.2.2.8">
<h4>
<span class="header-section-number">1.2.2.8</span> <code>Cis</code>：需要展示哪些顺式作用元件。<a class="anchor" aria-label="anchor" href="#cis%E9%9C%80%E8%A6%81%E5%B1%95%E7%A4%BA%E5%93%AA%E4%BA%9B%E9%A1%BA%E5%BC%8F%E4%BD%9C%E7%94%A8%E5%85%83%E4%BB%B6"><i class="fas fa-link"></i></a>
</h4>
</div>
<div id="plotgenestructure" class="section level4" number="1.2.2.9">
<h4>
<span class="header-section-number">1.2.2.9</span> <code>plotGeneStructure</code><a class="anchor" aria-label="anchor" href="#plotgenestructure"><i class="fas fa-link"></i></a>
</h4>
<p>该函数用于可视化基因结构。输入文件为<code>.gtf</code>文件。
如果同时输入对应的进化树文件，那么就会按照进化树的顺序对基因名称进行排序，可以和<code>ggtree</code>进行联用。</p>
</div>
<div id="fasta2df" class="section level4" number="1.2.2.10">
<h4>
<span class="header-section-number">1.2.2.10</span> <code>fasta2df</code><a class="anchor" aria-label="anchor" href="#fasta2df"><i class="fas fa-link"></i></a>
</h4>
<p>这个函数主要是为了将<code>fasta</code>格式转换成R中常见的<code>data.frame</code>格式。使用很简单，输入<code>fasta</code>格式即可。</p>
</div>
<div id="df2fasta" class="section level4" number="1.2.2.11">
<h4>
<span class="header-section-number">1.2.2.11</span> <code>df2fasta</code><a class="anchor" aria-label="anchor" href="#df2fasta"><i class="fas fa-link"></i></a>
</h4>
<p>这个函数主要是将<code>data.frame</code>格式的序列文件转换成<code>fasta</code>格式。</p>
</div>
</div>
</div>
<div id="ggmotif" class="section level2" number="1.3">
<h2>
<span class="header-section-number">1.3</span> R包ggmotif的使用方法<a class="anchor" aria-label="anchor" href="#ggmotif"><i class="fas fa-link"></i></a>
</h2>
<p>激发我开发这个R包的原因是在许做基因家族分析中的<code>motif</code>分析的时候总是找不到趁手的工具去可视化相关的结果，要么是在线网站，要么是别人开发的工具，网页版工具怕哪天就跑路了，别人的工具想要个性化比较困难。那，就自己写一个吧。</p>
<p>R包下载安装：</p>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op">(</span><span class="st">"ggmotif"</span><span class="op">)</span>

<span class="co"># 或者</span>NA<span class="fu">devtools</span><span class="fu">::</span><span class="fu"><a href="https://devtools.r-lib.org//reference/remote-reexports.html">install_github</a></span><span class="op">(</span><span class="st">"lixiang117423/ggmotif"</span><span class="op">)</span></code></pre></div>
<p><code>meme</code>输出的结果主要是<code>XML</code>格式和<code>txt</code>格式的文件，需要的结果就在这两个文件里面。</p>
<div id="从xml文件提取结果并绘图" class="section level3" number="1.3.1">
<h3>
<span class="header-section-number">1.3.1</span> 从XML文件提取结果并绘图<a class="anchor" aria-label="anchor" href="#%E4%BB%8Exml%E6%96%87%E4%BB%B6%E6%8F%90%E5%8F%96%E7%BB%93%E6%9E%9C%E5%B9%B6%E7%BB%98%E5%9B%BE"><i class="fas fa-link"></i></a>
</h3>
<p><code>motif</code>在序列上的位置结果存放在<code>XML</code>格式的文件中，运行下面的代码便可以将结果转换成数据框格式并进行可视化。</p>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">filepath</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples"</span>, <span class="st">"meme.xml"</span>, package<span class="op">=</span><span class="st">"ggmotif"</span><span class="op">)</span>
<span class="va">motif_extract</span> <span class="op">&lt;-</span> <span class="fu">getMotifFromXML</span><span class="op">(</span>XMLfile <span class="op">=</span> <span class="va">filepath</span><span class="op">)</span>
<span class="va">motif_plot</span> <span class="op">&lt;-</span> <span class="fu">motif_location</span><span class="op">(</span>data <span class="op">=</span> <span class="va">motif_extract</span><span class="op">)</span>
<span class="va">motif_plot</span></code></pre></div>
</div>
<div id="从txt文件提取结果并进行可视化" class="section level3" number="1.3.2">
<h3>
<span class="header-section-number">1.3.2</span> 从txt文件提取结果并进行可视化<a class="anchor" aria-label="anchor" href="#%E4%BB%8Etxt%E6%96%87%E4%BB%B6%E6%8F%90%E5%8F%96%E7%BB%93%E6%9E%9C%E5%B9%B6%E8%BF%9B%E8%A1%8C%E5%8F%AF%E8%A7%86%E5%8C%96"><i class="fas fa-link"></i></a>
</h3>
<p>每个<code>motif</code>的序列是存放在<code>txt</code>文件中的，运行下方的代码便能结果转换成数据框格式并进行绘图。</p>
<div class="sourceCode" id="cb9"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://tidyverse.tidyverse.org">tidyverse</a></span><span class="op">)</span>

<span class="va">filepath</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples"</span>, <span class="st">"meme.txt"</span>, package <span class="op">=</span> <span class="st">"ggmotif"</span><span class="op">)</span>
<span class="va">motif_extract</span> <span class="op">&lt;-</span> <span class="fu">getMotifFromTxt</span><span class="op">(</span>data <span class="op">=</span> <span class="va">filepath</span><span class="op">)</span>

<span class="va">motif_extract</span> <span class="op">%&gt;%</span> 
  <span class="fu">dplyr</span><span class="fu">::</span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/filter.html">filter</a></span><span class="op">(</span><span class="va">motif.num</span> <span class="op">==</span> <span class="st">'Motif.1'</span><span class="op">)</span> <span class="op">%&gt;%</span> 
  <span class="fu">dplyr</span><span class="fu">::</span><span class="fu"><a href="https://dplyr.tidyverse.org/reference/select.html">select</a></span><span class="op">(</span><span class="va">input.seq.motif</span><span class="op">)</span> <span class="op">%&gt;%</span> 
  <span class="fu">ggseqlogo</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ggseqlogo/man/ggseqlogo.html">ggseqlogo</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme_classic</span><span class="op">(</span><span class="op">)</span></code></pre></div>
</div>
<div id="与ggtree联用" class="section level3" number="1.3.3">
<h3>
<span class="header-section-number">1.3.3</span> 与<code>ggtree</code>联用<a class="anchor" aria-label="anchor" href="#%E4%B8%8Eggtree%E8%81%94%E7%94%A8"><i class="fas fa-link"></i></a>
</h3>
<p>有时候会对序列构建进化树展示进化关系，<code>ggmotif</code>中的函数能够以输入的进化树文件为基础，把输出的<code>motif</code>在序列上的位置与进化树上的位置进行匹配，便能够与<code>ggtree</code>联用在一个图上展示进化树和<code>motif</code>在序列上的位置。</p>
<div class="sourceCode" id="cb10"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">filepath</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples"</span>, <span class="st">"meme.xml"</span>, package<span class="op">=</span><span class="st">"ggmotif"</span><span class="op">)</span>
<span class="va">treepath</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/r/base/system.file.html">system.file</a></span><span class="op">(</span><span class="st">"examples"</span>, <span class="st">"tree.nwk"</span>, package<span class="op">=</span><span class="st">"ggmotif"</span><span class="op">)</span>
<span class="va">motif_extract</span> <span class="op">&lt;-</span> <span class="fu">getMotifFromXML</span><span class="op">(</span>XMLfile <span class="op">=</span> <span class="va">filepath</span><span class="op">)</span>
<span class="va">motif_plot_with_tree</span> <span class="op">&lt;-</span> <span class="fu">motif_with_tree</span><span class="op">(</span>data <span class="op">=</span> <span class="va">motif_extract</span>, tree <span class="op">=</span> <span class="va">treepath</span><span class="op">)</span>

<span class="va">p.tree</span> <span class="op">=</span> <span class="fu">ape</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ape/man/read.tree.html">read.tree</a></span><span class="op">(</span><span class="va">treepath</span><span class="op">)</span> <span class="op">%&gt;%</span> 
  <span class="fu">ggtree</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/ggtree.html">ggtree</a></span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">ggtree</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/ggtree/man/geom_tiplab.html">geom_tiplab</a></span><span class="op">(</span><span class="op">)</span>

<span class="va">p.tree</span>  <span class="op">%&gt;%</span> 
  <span class="fu">aplot</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/aplot/man/plot-insertion.html">insert_right</a></span><span class="op">(</span><span class="va">motif_plot_with_tree</span>, width <span class="op">=</span> <span class="fl">0.8</span><span class="op">)</span></code></pre></div>
</div>
</div>
<div id="ggplot2绘图相关" class="section level2" number="1.4">
<h2>
<span class="header-section-number">1.4</span> <code>ggplot2</code>绘图相关<a class="anchor" aria-label="anchor" href="#ggplot2%E7%BB%98%E5%9B%BE%E7%9B%B8%E5%85%B3"><i class="fas fa-link"></i></a>
</h2>
<div id="ggplot2绘制桑基冲击图" class="section level3" number="1.4.1">
<h3>
<span class="header-section-number">1.4.1</span> <code>ggplot2</code>绘制桑基(冲击图)<a class="anchor" aria-label="anchor" href="#ggplot2%E7%BB%98%E5%88%B6%E6%A1%91%E5%9F%BA%E5%86%B2%E5%87%BB%E5%9B%BE"><i class="fas fa-link"></i></a>
</h3>
<div class="sourceCode" id="cb11"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://ggforce.data-imaginist.com">ggforce</a></span><span class="op">)</span>
<span class="va">titanic</span> <span class="op">&lt;-</span> <span class="fu">reshape2</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/reshape2/man/melt.html">melt</a></span><span class="op">(</span><span class="va">Titanic</span><span class="op">)</span>
<span class="co"># This is how we usually envision data for parallel sets</span>
<span class="co">#head(titanic)</span>
<span class="co"># Reshape for putting the first 4 columns as axes in the plot</span>
<span class="va">titanic</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://ggforce.data-imaginist.com/reference/gather_set_data.html">gather_set_data</a></span><span class="op">(</span><span class="va">titanic</span>, <span class="fl">1</span><span class="op">:</span><span class="fl">4</span><span class="op">)</span>
<span class="co">#head(titanic)</span>
<span class="co"># Do the plotting</span>
<span class="fu">ggplot</span><span class="op">(</span><span class="va">titanic</span>, <span class="fu">aes</span><span class="op">(</span><span class="va">x</span>, id <span class="op">=</span> <span class="va">id</span>, split <span class="op">=</span> <span class="va">y</span>, value <span class="op">=</span> <span class="va">value</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggforce.data-imaginist.com/reference/geom_parallel_sets.html">geom_parallel_sets</a></span><span class="op">(</span><span class="fu">aes</span><span class="op">(</span>fill <span class="op">=</span> <span class="va">Sex</span><span class="op">)</span>, alpha <span class="op">=</span> <span class="fl">0.3</span>, axis.width <span class="op">=</span> <span class="fl">0.1</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggforce.data-imaginist.com/reference/geom_parallel_sets.html">geom_parallel_sets_axes</a></span><span class="op">(</span>axis.width <span class="op">=</span> <span class="fl">0.2</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu"><a href="https://ggforce.data-imaginist.com/reference/geom_parallel_sets.html">geom_parallel_sets_labels</a></span><span class="op">(</span>colour <span class="op">=</span> <span class="st">'white'</span>,angle <span class="op">=</span> <span class="fl">0</span>, size <span class="op">=</span> <span class="fl">6</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_y_continuous</span><span class="op">(</span>expand <span class="op">=</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">0</span>,<span class="fl">0</span><span class="op">)</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">scale_fill_aaas</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme_minimal</span><span class="op">(</span><span class="op">)</span> <span class="op">+</span>
  <span class="fu">theme</span><span class="op">(</span>axis.text.y <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span>,
        axis.text.x <span class="op">=</span> <span class="fu">element_text</span><span class="op">(</span>size <span class="op">=</span> <span class="fl">12</span><span class="op">)</span>,
        axis.title <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span>,
        panel.background <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span>,
        panel.grid <span class="op">=</span> <span class="fu">element_blank</span><span class="op">(</span><span class="op">)</span><span class="op">)</span></code></pre></div>
<div class="figure" style="text-align: center">
<span id="fig:ggplot4SankeyDiagram"></span>
<img src="001-R%E8%AF%AD%E8%A8%80_files/figure-html/ggplot4SankeyDiagram-1.png" alt="ggplot2绘制桑基图示例" width="672"><p class="caption">
图1.4: ggplot2绘制桑基图示例
</p>
</div>
</div>
</div>
<div id="服务器搭建rshiny-server" class="section level2" number="1.5">
<h2>
<span class="header-section-number">1.5</span> 服务器搭建Rshiny-server<a class="anchor" aria-label="anchor" href="#%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BArshiny-server"><i class="fas fa-link"></i></a>
</h2>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="r.html#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="co">#root 权限进入</span></span>
<span id="cb12-2"><a href="r.html#cb12-2" aria-hidden="true" tabindex="-1"></a>sudo su</span>
<span id="cb12-3"><a href="r.html#cb12-3" aria-hidden="true" tabindex="-1"></a>useradd shiny <span class="co"># 添加shiny用户</span></span>
<span id="cb12-4"><a href="r.html#cb12-4" aria-hidden="true" tabindex="-1"></a>passwd shiny <span class="co"># 修改shiny用户的密码</span></span>
<span id="cb12-5"><a href="r.html#cb12-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-6"><a href="r.html#cb12-6" aria-hidden="true" tabindex="-1"></a>chmod <span class="sc">+</span>w <span class="sc">/</span>etc<span class="sc">/</span>sudoers <span class="co"># 修改权限</span></span>
<span id="cb12-7"><a href="r.html#cb12-7" aria-hidden="true" tabindex="-1"></a>sudo vim <span class="sc">/</span>etc<span class="sc">/</span>sudoers <span class="co"># 编辑</span></span>
<span id="cb12-8"><a href="r.html#cb12-8" aria-hidden="true" tabindex="-1"></a><span class="co"># root ALL=(ALL) ALL 该行，在下面添加 shiny ALL=(ALL:ALL) ALL</span></span>
<span id="cb12-9"><a href="r.html#cb12-9" aria-hidden="true" tabindex="-1"></a>chmod <span class="sc">-</span>w <span class="sc">/</span>etc<span class="sc">/</span>sudoers <span class="co"># 收回权限</span></span>
<span id="cb12-10"><a href="r.html#cb12-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-11"><a href="r.html#cb12-11" aria-hidden="true" tabindex="-1"></a>su <span class="sc">-</span> shiny <span class="co"># 切换到shiny用户</span></span>
<span id="cb12-12"><a href="r.html#cb12-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-13"><a href="r.html#cb12-13" aria-hidden="true" tabindex="-1"></a><span class="co"># 安装R</span></span>
<span id="cb12-14"><a href="r.html#cb12-14" aria-hidden="true" tabindex="-1"></a>sudo apt<span class="sc">-</span>get update</span>
<span id="cb12-15"><a href="r.html#cb12-15" aria-hidden="true" tabindex="-1"></a>sudo apt<span class="sc">-</span>get install r<span class="sc">-</span>base</span>
<span id="cb12-16"><a href="r.html#cb12-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-17"><a href="r.html#cb12-17" aria-hidden="true" tabindex="-1"></a><span class="co"># 安装shiny包</span></span>
<span id="cb12-18"><a href="r.html#cb12-18" aria-hidden="true" tabindex="-1"></a>sudo su <span class="sc">-</span>c <span class="st">"R -e </span><span class="sc">\"</span><span class="st">install.packages('shiny',repos='https://cran.rstudio.com/')</span><span class="sc">\"</span><span class="st">"</span></span>
<span id="cb12-19"><a href="r.html#cb12-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb12-20"><a href="r.html#cb12-20" aria-hidden="true" tabindex="-1"></a><span class="co"># 安装shiny server</span></span>
<span id="cb12-21"><a href="r.html#cb12-21" aria-hidden="true" tabindex="-1"></a>sudo apt<span class="sc">-</span>get install gdebi<span class="sc">-</span>core</span>
<span id="cb12-22"><a href="r.html#cb12-22" aria-hidden="true" tabindex="-1"></a>wget https<span class="sc">:</span><span class="er">//</span>download3.rstudio.org<span class="sc">/</span>ubuntu<span class="fl">-14.04</span><span class="sc">/</span>x86_64<span class="sc">/</span>shiny<span class="sc">-</span>server<span class="dv">-1</span>.<span class="dv">5</span>.<span class="fl">17.973</span><span class="sc">-</span>amd64.deb</span>
<span id="cb12-23"><a href="r.html#cb12-23" aria-hidden="true" tabindex="-1"></a>sudo gdebi shiny<span class="sc">-</span>server<span class="dv">-1</span>.<span class="dv">5</span>.<span class="fl">17.973</span><span class="sc">-</span>amd64.deb</span></code></pre></div>
<p>安装shiny-server的时候参考官方最新的指导页面，<a href="https://www.rstudio.com/products/shiny/download-server/ubuntu/">点击访问</a>。</p>
<p>日志所在目录为：<code>/var/log/shiny-server</code>。需要修改配置文件，保证记录运行日志，否则可能找不到报错提示。</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="r.html#cb13-1" aria-hidden="true" tabindex="-1"></a>vi <span class="sc">/</span>etc<span class="sc">/</span>shiny<span class="sc">-</span>server<span class="sc">/</span>shiny<span class="sc">-</span>server.conf</span>
<span id="cb13-2"><a href="r.html#cb13-2" aria-hidden="true" tabindex="-1"></a>preserve_logs true; <span class="co"># 在最上面添加这行</span></span></code></pre></div>
<p>软件所在目录为：<code>/srv/shiny-server</code></p>
<p>安装完成后在浏览器输入<code>IP:3838</code>就能访问到示例界面了。默认的端口是<code>3838</code>端口，需要其他端口的需要自行调整。</p>
<p>基本管理：</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="r.html#cb14-1" aria-hidden="true" tabindex="-1"></a><span class="do">## 重启</span></span>
<span id="cb14-2"><a href="r.html#cb14-2" aria-hidden="true" tabindex="-1"></a>sudo systemctl restart shiny<span class="sc">-</span>server</span>
<span id="cb14-3"><a href="r.html#cb14-3" aria-hidden="true" tabindex="-1"></a><span class="do">##查看状态 </span></span>
<span id="cb14-4"><a href="r.html#cb14-4" aria-hidden="true" tabindex="-1"></a>sudo systemctl status shiny<span class="sc">-</span>server </span>
<span id="cb14-5"><a href="r.html#cb14-5" aria-hidden="true" tabindex="-1"></a><span class="do">##开启 </span></span>
<span id="cb14-6"><a href="r.html#cb14-6" aria-hidden="true" tabindex="-1"></a>sudo systemctl start shiny<span class="sc">-</span>server </span>
<span id="cb14-7"><a href="r.html#cb14-7" aria-hidden="true" tabindex="-1"></a><span class="do">##停止 </span></span>
<span id="cb14-8"><a href="r.html#cb14-8" aria-hidden="true" tabindex="-1"></a>sudo systemctl stop shiny<span class="sc">-</span>server </span></code></pre></div>
</div>
<div id="ubuntu安装devtools" class="section level2" number="1.6">
<h2>
<span class="header-section-number">1.6</span> Ubuntu安装devtools<a class="anchor" aria-label="anchor" href="#ubuntu%E5%AE%89%E8%A3%85devtools"><i class="fas fa-link"></i></a>
</h2>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="r.html#cb15-1" aria-hidden="true" tabindex="-1"></a>sudo apt<span class="sc">-</span>get install <span class="sc">-</span>y libxml2<span class="sc">-</span>dev libcurl4<span class="sc">-</span>openssl<span class="sc">-</span>dev libssl<span class="sc">-</span>devv</span></code></pre></div>
<p>最重要的是上面这一步，剩下的差啥装啥就OK。</p>
<p>安装<code>proj</code>这个包：</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="r.html#cb16-1" aria-hidden="true" tabindex="-1"></a>sudo apt<span class="sc">-</span>get install libproj<span class="sc">-</span>dev</span>
<span id="cb16-2"><a href="r.html#cb16-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-3"><a href="r.html#cb16-3" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="st">"RcppEigen"</span>, <span class="at">INSTALL_opts =</span> <span class="st">'--no-lock'</span>)</span></code></pre></div>
</div>
<div id="r-shiny开发" class="section level2" number="1.7">
<h2>
<span class="header-section-number">1.7</span> R shiny开发<a class="anchor" aria-label="anchor" href="#r-shiny%E5%BC%80%E5%8F%91"><i class="fas fa-link"></i></a>
</h2>
<div id="css学习网站" class="section level3" number="1.7.1">
<h3>
<span class="header-section-number">1.7.1</span> CSS学习网站<a class="anchor" aria-label="anchor" href="#css%E5%AD%A6%E4%B9%A0%E7%BD%91%E7%AB%99"><i class="fas fa-link"></i></a>
</h3>
<p><a href="https://www.runoob.com/css/css-align.html">CSS菜鸟教程</a>。</p>
<p><a href="https://unleash-shiny.rinterface.com/beautify-css.html">CSS在Rshiny中的使用</a>。</p>

</div>
</div>
</div>
  <div class="chapter-nav">
<div class="prev"><a href="index.html">简介</a></div>
<div class="next"><a href="python.html"><span class="header-section-number">2</span> Python知识汇总</a></div>
</div></main><div class="col-md-3 col-lg-2 d-none d-md-block sidebar sidebar-chapter">
    <nav id="toc" data-toggle="toc" aria-label="On this page"><h2>On this page</h2>
      <ul class="nav navbar-nav">
<li><a class="nav-link" href="#r"><span class="header-section-number">1</span> R语言知识汇总</a></li>
<li><a class="nav-link" href="#%E6%9C%AC%E7%AB%A0%E5%89%8D%E8%A8%80"><span class="header-section-number">1.1</span> 本章前言</a></li>
<li>
<a class="nav-link" href="#pac4xiang"><span class="header-section-number">1.2</span> R包pac4xiang的使用</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#%E5%AE%89%E8%A3%85"><span class="header-section-number">1.2.1</span> 安装</a></li>
<li><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95"><span class="header-section-number">1.2.2</span> 使用方法</a></li>
</ul>
</li>
<li>
<a class="nav-link" href="#ggmotif"><span class="header-section-number">1.3</span> R包ggmotif的使用方法</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#%E4%BB%8Exml%E6%96%87%E4%BB%B6%E6%8F%90%E5%8F%96%E7%BB%93%E6%9E%9C%E5%B9%B6%E7%BB%98%E5%9B%BE"><span class="header-section-number">1.3.1</span> 从XML文件提取结果并绘图</a></li>
<li><a class="nav-link" href="#%E4%BB%8Etxt%E6%96%87%E4%BB%B6%E6%8F%90%E5%8F%96%E7%BB%93%E6%9E%9C%E5%B9%B6%E8%BF%9B%E8%A1%8C%E5%8F%AF%E8%A7%86%E5%8C%96"><span class="header-section-number">1.3.2</span> 从txt文件提取结果并进行可视化</a></li>
<li><a class="nav-link" href="#%E4%B8%8Eggtree%E8%81%94%E7%94%A8"><span class="header-section-number">1.3.3</span> 与ggtree联用</a></li>
</ul>
</li>
<li>
<a class="nav-link" href="#ggplot2%E7%BB%98%E5%9B%BE%E7%9B%B8%E5%85%B3"><span class="header-section-number">1.4</span> ggplot2绘图相关</a><ul class="nav navbar-nav"><li><a class="nav-link" href="#ggplot2%E7%BB%98%E5%88%B6%E6%A1%91%E5%9F%BA%E5%86%B2%E5%87%BB%E5%9B%BE"><span class="header-section-number">1.4.1</span> ggplot2绘制桑基(冲击图)</a></li></ul>
</li>
<li><a class="nav-link" href="#%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BArshiny-server"><span class="header-section-number">1.5</span> 服务器搭建Rshiny-server</a></li>
<li><a class="nav-link" href="#ubuntu%E5%AE%89%E8%A3%85devtools"><span class="header-section-number">1.6</span> Ubuntu安装devtools</a></li>
<li>
<a class="nav-link" href="#r-shiny%E5%BC%80%E5%8F%91"><span class="header-section-number">1.7</span> R shiny开发</a><ul class="nav navbar-nav"><li><a class="nav-link" href="#css%E5%AD%A6%E4%B9%A0%E7%BD%91%E7%AB%99"><span class="header-section-number">1.7.1</span> CSS学习网站</a></li></ul>
</li>
</ul>

      <div class="book-extra">
        <ul class="list-unstyled">
<li><a id="book-source" href="https://github.com/lixiang117423/lixiang117423.github.io/blob/master/001-R%E8%AF%AD%E8%A8%80.Rmd">View source <i class="fab fa-github"></i></a></li>
          <li><a id="book-edit" href="https://github.com/lixiang117423/lixiang117423.github.io/edit/master/001-R%E8%AF%AD%E8%A8%80.Rmd">Edit this page <i class="fab fa-github"></i></a></li>
        </ul>
</div>
    </nav>
</div>

</div>
</div> <!-- .container -->

<footer class="bg-primary text-light mt-5"><div class="container"><div class="row">

  <div class="col-12 col-md-6 mt-3">
    <p>"<strong>小蓝哥的知识荒原</strong>" was written by 李详. It was last built on 2021年10月1日.</p>
  </div>

  <div class="col-12 col-md-6 mt-3">
    <p>This book was built by the <a class="text-light" href="https://bookdown.org">bookdown</a> R package.</p>
  </div>

</div></div>
</footer><!-- dynamically load mathjax for compatibility with self-contained --><script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>
</html>
